<template>
<!--  <d2-container class="mod-sys__user">-->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入系统合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同签署流程状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择合同签署流程状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入合同金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同付费金额" prop="contractAmount">
        <el-input
          v-model="queryParams.contractAmount"
          placeholder="请输入合同付费金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同所属公司" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入合同所属公司"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同所属公司名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入合同所属公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同签署客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入合同签署客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:contract:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border stripe :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractNo" >
        <template slot-scope="scope">
          <el-button type="text" @click="contractDetailList(scope.row)">{{ scope.row.contractNo }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="合同状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" size="mini" type="info">{{ $t('contract.status0') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" size="mini" type="warning">{{ $t('contract.status1') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" size="mini">{{ $t('contract.status2') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '3'" size="mini" type="warning">{{ $t('contract.status3') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '4'" size="mini" type="success">{{ $t('contract.status4') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '5'" size="mini" type="danger">{{ $t('contract.status5') }}</el-tag>
          <el-tag v-else-if="scope.row.status === '6'" size="mini" type="danger">{{ $t('contract.status6') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="datumStatus" label="资料上传状态" header-align="center" align="center" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.datumStatus === 0" size="mini" type="danger">{{ $t('contract.datumStatus0') }}</el-tag>
          <el-tag v-else-if="scope.row.datumStatus === 1" size="mini" type="success">{{$t('contract.datumStatus1') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="amount" />
      <el-table-column label="合同付费金额" align="center" prop="contractAmount" />
      <el-table-column label="合同所属公司名称" align="center" prop="clientName" />
      <el-table-column label="合同签署客户名称" align="center" prop="customerName" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" prop="createTime"  header-align="center" align="center" width="90"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:contract:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:contract:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改合同主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="系统合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入系统合同编号" />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="合同签署流程状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择合同签署流程状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入合同金额" />
        </el-form-item>
        <el-form-item label="合同付费金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同付费金额" />
        </el-form-item>
        <el-form-item label="微信支付订单号" prop="wechatNo">
          <el-input v-model="form.wechatNo" placeholder="请输入微信支付订单号" />
        </el-form-item>
        <el-form-item label="是否已支付" prop="isPay">
          <el-input v-model="form.isPay" placeholder="请输入是否已支付" />
        </el-form-item>
        <el-form-item label="上上签合同签署url" prop="contractUrl">
          <el-input v-model="form.contractUrl" placeholder="请输入上上签合同签署url" />
        </el-form-item>
        <el-form-item label="合同所属公司" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入合同所属公司" />
        </el-form-item>
        <el-form-item label="合同所属公司名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入合同所属公司名称" />
        </el-form-item>
        <el-form-item label="合同签署客户id" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入合同签署客户id" />
        </el-form-item>
        <el-form-item label="合同签署客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入合同签署客户名称" />
        </el-form-item>
        <el-form-item label="资料上传状态:">
          <el-radio-group v-model="form.datumStatus">
            <el-radio
              v-for="dict in datumStatusOptions"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.name}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上上签合同id(废弃字段)" prop="bestsignContractId">
          <el-input v-model="form.bestsignContractId" placeholder="请输入上上签合同id(废弃字段)" />
        </el-form-item>
        <el-form-item label="逻辑删除 0-删除, 默认-1" prop="valid">
          <el-input v-model="form.valid" placeholder="请输入逻辑删除 0-删除, 默认-1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
<!--  </d2-container>-->
</template>

<script>
import { listContract, getContract, delContract, updateContract, exportContract } from "@/api/zfw/contract/contract";

export default {
  name: "Contract",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同主表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 合同签署流程状态字典
      statusOptions: [],
      // 资料上传状态: 0- 未完成 1-已完成字典
      datumStatusOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        contractNo: null,
        contractName: null,
        status: null,
        amount: null,
        contractAmount: null,
        clientId: null,
        clientName: null,
        customerName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_contract_flow_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_contract_datum_status").then(response => {
      this.datumStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询合同主列表 */
    getList() {
      this.loading = true;
      listContract(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.contractList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 合同签署流程状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 资料上传状态: 0- 未完成 1-已完成字典翻译
    datumStatusFormat(row, column) {
      return this.selectDictLabel(this.datumStatusOptions, row.datumStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNo: null,
        contractName: null,
        status: null,
        amount: null,
        contractAmount: null,
        wechatNo: null,
        isPay: null,
        contractUrl: null,
        clientId: null,
        clientName: null,
        customerId: null,
        customerName: null,
        datumStatus: 0,
        bestsignContractId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        valid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合同主编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContract(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同主数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportContract(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
